package com.yunnuo.cmsmessage.service.impl;

import com.yunnuo.cmsmessage.entity.CMSMessage;
import com.yunnuo.system.comm.util.Empty;
import com.yunnuo.system.comm.util.ShiroUtils;
import com.yunnuo.system.comm.util.page.TableDataInfo;
import com.yunnuo.cmsmessage.dao.IMessageDao;
import com.yunnuo.cmsmessage.service.IMessageService;
import com.yunnuo.system.role.dao.IRoleDao;
import com.yunnuo.system.role.entity.Role;
import com.yunnuo.system.users.entity.PageParams;
import com.yunnuo.system.users.entity.User;
import org.hibernate.service.spi.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 魏洪福（15064000912@163.com）
 * @create 2018-08-27-16:05
 */
@Service
public  class MessageServiceImpl implements IMessageService {

    @Autowired
    private IMessageDao messageDao;

    @Autowired
    private IRoleDao roleDao;

    @Override
    public void addVO(CMSMessage vo) throws ServiceException {

    }

    @Override
    public void updateVO(CMSMessage vo) throws ServiceException {

    }

    @Override
    public void delList(String[] ids) throws ServiceException {

    }

    @Override
    public void deleteVo(String id) throws ServiceException {

    }

    @Override
    public List<CMSMessage> findList() throws ServiceException {
        return null;
    }

    @Override
    public List<?> findList(CMSMessage vo, String other) throws ServiceException {
        return null;
    }

    @Override
    public CMSMessage findVO(String id) throws ServiceException {
        return null;
    }

    /**
     * 分页查询
     * @param pageParams
     * @param pageRequest
     * @return
     */
    @Override
    public TableDataInfo selectMessageByPageParams(PageParams pageParams, PageRequest pageRequest) {
        TableDataInfo tableDataInfo=new TableDataInfo();
        Specification<CMSMessage> specification=new Specification<CMSMessage>() {
            @Override
            public Predicate toPredicate(Root<CMSMessage> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<String> namePath=root.get("title");
                String value=pageParams.getSearchValue();
                if(Empty.isNotEmpty(value)){
                    query.where(cb.like(namePath,"%"+value+"%"));
                }
                return null;
            }
        };
        Page<CMSMessage> messages=  messageDao.findAll(specification,pageRequest);
        tableDataInfo.setTotal(messages.getTotalElements());
        tableDataInfo.setRows(messages.getContent());
        return tableDataInfo;
    }

    /**
     * 查询用户对应的消息
     * @param pageParams
     * @param pageRequest
     * @return
     */
    @Override
    public TableDataInfo selectMessageByUserAndPageParams(PageParams pageParams, PageRequest pageRequest) {
        User user= ShiroUtils.getUser();
        TableDataInfo tableDataInfo=new TableDataInfo();
        Specification<CMSMessage> specification=new Specification<CMSMessage>() {
            @Override
            public Predicate toPredicate(Root<CMSMessage> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<String> rightUserId=root.get("rightUserId");
                Path<String> rightDeptId=root.get("rightDeptId");
                Path<String> rightRoleId=root.get("rightRoleId");
                String userId=user.getId();
                String deptId=user.getDeptId();
                List<Role> roles=roleDao.selectAllRoleByUserId(user.getId());
                List<Predicate> predicates=new ArrayList<>();

                if(Empty.isNotEmpty(userId)&&Empty.isNotEmpty(deptId)&&Empty.isNotEmpty(roles)){
                    query.where(cb.and(cb.like(rightUserId,"%"+userId+"%"),cb.like(rightDeptId,"%"+deptId+"%"),cb.like(rightRoleId,"%")));
                }
                return null;
            }
        };
        Page<CMSMessage> messages=  messageDao.findAll(specification,pageRequest);
        tableDataInfo.setTotal(messages.getTotalElements());
        tableDataInfo.setRows(messages.getContent());
        return tableDataInfo;
    }

}
